package org.flynenu.core.dao.base;

import org.flynenu.core.dao.session.SessionBuilder;
import org.hibernate.Session;


public abstract class BaseDao {
	private Session session = null;
	
	private static SessionBuilder builder = null;
	
	public void setSession(SessionBuilder session) {
		if(builder == null)
			builder = session;
		
		this.session = builder.buildeSession();
	}

	protected Session session(){
		if(!session.isOpen()){
			session = builder.buildeSession();
		}
		
		return session;
	}
	
	public void beginTransaction(){
		if(!session.isOpen()){
			session = builder.buildeSession();
		}
		
		session.beginTransaction();
	}
	
	public void commint(){
		session().getTransaction().commit();
	}
	
	public void open(){
		if(!session().isOpen()){
			session = builder.buildeSession();
		}
	}
	
	public void close(){
		session().close();
	}
}
